package com.yidu.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * <p>@Description: 常用的工具类</p>
 * <p>@Author yidu-liandyao</p>
 * <p>@Date: 2020-11-17</p>
 * <p>@Time: 9:00</p>
 */
public class Tools {

    /**
     * 传入一个String,判断该对象是否为空
     * @param obj
     * @return 如果为空返回true,否则返回false
     */
    public static boolean isEmpty(String obj){

        if(obj==null || "".equals(obj)){
            return true ;
        }else{
            return false ;
        }

    }

    static Random rd = new Random();

    public static String getRandom(){
        long time = System.currentTimeMillis();

        time = time/(rd.nextInt(10)+1);

        int num = rd.nextInt(10000000);

        String str = time+"_"+num ;
        return str ;
    }


    /**
     * 写出json的工具
     * @param response
     * @param obj
     */
    public static void write(HttpServletResponse response,Object obj) throws IOException {
        //json转换日期时,格式规范为yyyy-MM-dd
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(obj);
        //输出json
        System.out.println(json);
        PrintWriter out = response.getWriter();
        out.print(json);

        out.close();
    }
}
